package com.pilipili.zeus.eduService.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduCourseCollect;
import com.pilipili.zeus.eduService.entity.excel.CourseSortExcelData;
import com.pilipili.zeus.eduService.service.EduCourseCollectService;

/**
 * @author Unlimited_Blade_Works
 */
public class CourseSortExcelListener extends AnalysisEventListener<CourseSortExcelData> {

        public EduCourseCollectService courseCollectService;

        public CourseSortExcelListener() {
        }

        public CourseSortExcelListener(EduCourseCollectService courseCollectService) {
            this.courseCollectService = courseCollectService;
        }

        //按行读取Excel内容
        @Override
        public void invoke(CourseSortExcelData courseSortData, AnalysisContext analysisContext) {
            if (courseSortData == null) {
                throw new MyException(20001, "文件数据为空");
            }
            EduCourseCollect isExistCourseSort = this.isExistCourseSort(courseCollectService, courseSortData.getCourseId(),courseSortData.getSubjectParentId());
            if (isExistCourseSort == null) {
                isExistCourseSort = new EduCourseCollect();
                isExistCourseSort.setCourseId(courseSortData.getCourseId());
                isExistCourseSort.setMemberId(courseSortData.getSubjectParentId());
                courseCollectService.save(isExistCourseSort);

            }
        }
        private EduCourseCollect isExistCourseSort(EduCourseCollectService courseCollectService, String id, String parentId){
            QueryWrapper<EduCourseCollect> wrapper=new QueryWrapper<>();
            wrapper.eq("course_id",id);
            wrapper.eq("member_id",parentId);
            return courseCollectService.getOne(wrapper);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }
    }

